#!/bin/bash

set -eu
# set -x

onnx_model=$1
om_name=$2
input_shape=$3
batch_size=$4
acc_out=$5
speed_out=$6
soc_version=Ascend310

# setup
# CANN安装目录
export install_path=/usr/local/Ascend/ascend-toolkit/latest
export PATH=/usr/local/python3.7.5/bin:${install_path}/atc/ccec_compiler/bin:${install_path}/atc/bin:$PATH
export PYTHONPATH=${install_path}/atc/python/site-packages:$PYTHONPATH
export LD_LIBRARY_PATH=${install_path}/atc/lib64:${install_path}/acllib/lib64:$LD_LIBRARY_PATH
export ASCEND_OPP_PATH=${install_path}/opp
export ASCEND_AICPU_PATH=$install_path
# 将atc日志打印到屏幕
export ASCEND_SLOG_PRINT_TO_STDOUT=1
# 设置日志级别
#export ASCEND_GLOBAL_LOG_LEVEL=0 #debug 0 --> info 1 --> warning 2 --> error 3
# 开启ge dump图
#export DUMP_GE_GRAPH=2

mkdir -p tmp

# convert model
echo 'convert om model...'
# atc --framework=5 --model=$onnx_model --output=$om_name --input_format=ND --input_shape=$input_shape --log=debug --soc_version=Ascend310 &> ./tmp/convert.log
echo 'convert finished!'

om_model=${om_name}.om
# acc check
mkdir -p ${acc_out}
echo 'acc checking...'
# bash acc_profiling/acc_profiling.sh ${onnx_model} ${om_model} ${acc_out} &> ${acc_out}/acc.log
echo 'acc checked!'
tail -n3 ${acc_out}/acc.log

# speed profiling
mkdir -p ${speed_out}
speed_out=`realpath ${speed_out}`
echo 'speed profiling...'
# bash speed_profiling/speed_profiling.sh ${om_model} ${batch_size} ${speed_out} &> tmp/speed.log  
echo 'profiling done, outputdir: '`realpath ${speed_out}/*`

# speed comparing
python speed_profiling/compare_profiling.py --input_csv_file=debug/speed_out/JOBDHIDGEJAHCBGCFHHICGBAAAAAAAAA/summary/op_summary_1_1_1.csv --input_json_file debug/FixRes_bs1.json --output_csv_file=debug/out.csv

rm -rf tmp
